﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 水牛管理系统.UI {
    public partial class Record : Form {

        public Record() {
            InitializeComponent();
            loadtab1();
            loadtab2();
            loadtab3();
        }

        private void Record_Load(object sender, EventArgs e) {
           // initRecord();
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            btn.Name = "btnModify";
            btn.HeaderText = " ";
            btn.DefaultCellStyle.NullValue = "修改";
            dataGridView1.Columns.Add(btn);
            DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn();
            btn1.Name = "btnDelete";
            btn1.HeaderText = " ";
            btn1.DefaultCellStyle.NullValue = "删除";
            dataGridView1.Columns.Add(btn1);
        }

        private void initRecord() {
            try {
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter("select log_id as 订单号,user.user_name as 姓名,log.user_card_id as 卡号,log_date as 日期,cost as 消费金额 from log left join user on log.user_card_id=user.user_card_id  group by log_date desc", new Utils.DatebaseUtil().open());
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception e1) {
                MessageBox.Show(e1.Message);
            }
        }

        private void queryData(object sender, EventArgs e) {
            try {
                String query = "'%" + textBox1.Text + "%'";
                String sql = "select log_id as 订单号,user.user_name as 姓名,log.user_card_id as 卡号,log_date as 日期" +
                    ",cost as 消费金额 from log left join user on log.user_card_id=user.user_card_id  " +
                    "where log_id like " + query + " or user_name" +
                    " like " + query + " or log.user_card_id like " + query + " or log_date like " + query + "group by log_date desc";
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(sql, new Utils.DatebaseUtil().open());
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception e1) {
                MessageBox.Show(e1.Message);
            }
        }

        private void cell_Click(object sender, DataGridViewCellEventArgs e) {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "btnDelete" && e.RowIndex >= 0) {
                //说明点击的列是DataGridViewButtonColumn列
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                MySqlCommand cmd = new MySqlCommand("delete from visitLog where log_id=@a", new Utils.DatebaseUtil().open());
                cmd.Parameters.AddWithValue("a", Convert.ToString(dataGridView1.CurrentRow.Cells[2].Value));
                int flag = cmd.ExecuteNonQuery();
                if (flag > 0) {
                    MessageBox.Show("删除成功！");
                   // initRecord();
                }
                else {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e) {

        }

        private void loadtab1() {
            try {
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter("select log_id as 订单号,user.user_name as 姓名,log.user_card_id as 卡号,log_date as 日期,cost as 消费金额" +
                    " from log left join user on log.user_card_id=user.user_card_id" +
                    " where date_format(log_date, '%Y-%m-%d')=date_format(DATE(NOW()), '%Y-%m-%d')  group by log_date desc", new Utils.DatebaseUtil().open());
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception e1) {
                MessageBox.Show(e1.Message);
            }
        }
        private void loadtab2() {
            try {
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter("select log_id as 订单号,user.user_name as 姓名,log.user_card_id as 卡号,log_date as 日期,cost as 消费金额" +
                    " from log left join user on log.user_card_id=user.user_card_id" +
                    " where date_format(log_date, '%Y-%m')=date_format(DATE(NOW()), '%Y-%m')  group by log_date desc", new Utils.DatebaseUtil().open());
                da.Fill(ds);
                dataGridView2.DataSource = ds.Tables[0];
            }
            catch (Exception e1) {
                MessageBox.Show(e1.Message);
            }
        }
        private void loadtab3() {
            try {
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter("select log_id as 订单号,user.user_name as 姓名,log.user_card_id as 卡号,log_date as 日期,cost as 消费金额" +
                    " from log left join user on log.user_card_id=user.user_card_id" +
                    " where date_format(log_date, '%Y')=date_format(DATE(NOW()), '%Y')  group by log_date desc", new Utils.DatebaseUtil().open());
                da.Fill(ds);
                dataGridView2.DataSource = ds.Tables[0];
            }
            catch (Exception e1) {
                MessageBox.Show(e1.Message);
            }
        }

        private void selectCount(object sender, EventArgs e) {
            int selectd = tabControl1.SelectedIndex;
            switch (selectd) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
        }
    }
}
